package com.haizhi.crm.biz.dao;

import com.haizhi.crm.biz.model.FavouriteDo;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.data.jpa.repository.Modifying;
import org.springframework.data.jpa.repository.Query;
import org.springframework.data.repository.query.Param;
import org.springframework.stereotype.Repository;

import java.util.List;

/**
 * Created by yangyijun on 2018/5/10.
 */
@Repository
public interface FavouriteDao extends JpaRepository<FavouriteDo, Long> {


    /**
     * 添加
     *
     * @param userId
     * @param entityType
     * @param entityKey
     */
    @Modifying
    @Query(value = "insert into tb_favourite(user_id, entity_type, entity_key) " +
            "values(:userId, :entityType, :entityKey)", nativeQuery = true)
    void insertFavourite(@Param("userId" ) Long userId, @Param("entityType" ) String entityType, @Param("entityKey" ) String entityKey);


    /**
     * 查询是否被收藏
     *
     * @param userId
     * @param entityType
     * @param entityKey
     * @return
     */
    @Query(value = "SELECT count(1) from tb_favourite t where t.user_id=:userId and t.entity_type=:entityType and t.entity_key=:entityKey ", nativeQuery = true)
    int findFavouriteCount(@Param("userId" ) Long userId, @Param("entityType" ) String entityType, @Param("entityKey" ) String entityKey);

    /**
     * 删除收藏信息
     *
     * @param entityType
     * @param entityKey
     */
    @Modifying
    @Query(value = "delete from tb_favourite where entity_type=:entityType and entity_key=:entityKey ", nativeQuery = true)
    void deleteByEntityTypeAndEntityID(@Param("entityType" ) String entityType, @Param("entityKey" ) String entityKey);

    /**
     * 删除收藏信息
     *
     * @param userId
     * @param entityType
     * @param entityKey
     */
    @Modifying
    @Query(value = "delete from tb_favourite where user_id=:userId and entity_type=:entityType and entity_key=:entityKey ", nativeQuery = true)
    void deleteByCondition(@Param("userId" ) Long userId, @Param("entityType" ) String entityType, @Param("entityKey" ) String entityKey);


    /**
     * 条件查询
     *
     * @param userId
     * @param entityType
     * @param entityKey
     * @return
     */
    @Query("select t " +
            "from FavouriteDo t  where t.userId=:userId and t.entityType=:entityType and t.entityKey=:entityKey " )
    FavouriteDo queryByCondition(@Param("userId" ) Long userId, @Param("entityType" ) String entityType, @Param("entityKey" ) String entityKey);


    /**
     * 查询收藏次数
     *
     * @param entityType
     * @param entityKey
     * @return
     */
    @Query(value = "select count(1) from tb_favourite  t where t.entity_type=:entityType and t.entity_key=:entityKey", nativeQuery = true)
    int queryByCondition(@Param("entityType" ) String entityType, @Param("entityKey" ) String entityKey);

    /**
     * 查询entitikey列表
     *
     * @param userIds
     * @param entityType
     */
    @Query(value = "select f.entityKey from FavouriteDo f where f.userId in :userIds and f.entityType=:entityType" )
    List<String> findEntityKeys(@Param("userIds" ) List<Long> userIds, @Param("entityType" ) String entityType);

}
